package com.xps.charmingshop.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.xps.charmingshop.R;
import com.xps.charmingshop.bean.MenuBean;
import com.xps.charmingshop.bean.OrderDetailsBean;
import com.xps.charmingshop.view.activity.UpdateMenuActivity;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

public class OrderDetailsListAdapter extends BaseExpandableListAdapter {
    private LayoutInflater inflater;
    private Context context;
    private ExpandableListView elv_collocation;
    private List<OrderDetailsBean.ReturnBodyBean.GoodsListBean> data;

    public OrderDetailsListAdapter(Context context, ExpandableListView elv_collocation, List<OrderDetailsBean.ReturnBodyBean.GoodsListBean> data) {
        this.context = context;
        this.elv_collocation = elv_collocation;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.data = data;
    }

    @Override
    public int getGroupCount() {
        return data.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return data.get(groupPosition).getGoods_must_optional().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return data.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return data.get(groupPosition).getGoods_must_optional().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;//如果子条目需要响应click事件,必需返回true
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentViewHolder parentViewHolder;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.adapter_orderdetails, parent, false);
            parentViewHolder = new ParentViewHolder(convertView);
            convertView.setTag(parentViewHolder);
            AutoUtils.auto(convertView);
        } else {
            parentViewHolder = (ParentViewHolder) convertView.getTag();
        }
        OrderDetailsBean.ReturnBodyBean.GoodsListBean collocationPackageBean = data.get(groupPosition);
        parentViewHolder.Complete_ID.setText(collocationPackageBean.getGoods_id());
        parentViewHolder.Complete_Money.setText(collocationPackageBean.getDish_name());
        parentViewHolder.Complete_Date.setText(collocationPackageBean.getDish_price());
        parentViewHolder.Complete_State.setText(collocationPackageBean.getGoods_num());
        parentViewHolder.Complete_Order.setText(collocationPackageBean.getGoods_dish_price());
        parentViewHolder.Iv_deletedish.setVisibility(View.VISIBLE);
        parentViewHolder.v_space.setVisibility(isExpanded ? View.GONE : View.VISIBLE);
        parentViewHolder.hsv_goods_list.setVisibility(isExpanded ? View.GONE : View.VISIBLE);
        parentViewHolder.hsv_goods_list.setFocusable(false);//设置后解决套餐无法正常展开的bug
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.adapter_orderdetails, parent, false);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
            AutoUtils.auto(convertView);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }

        childViewHolder.ivTubiao.setVisibility(View.VISIBLE);
        final OrderDetailsBean.ReturnBodyBean.GoodsListBean.GoodsMustOptionalBean collocationSkuBean = data.get(groupPosition).getGoods_must_optional().get(childPosition);

        if (collocationSkuBean.getMust_name() == "" || collocationSkuBean.getMust_name() == null) {
            childViewHolder.Complete_Money.setText(collocationSkuBean.getOptional_name());
            childViewHolder.Complete_Date.setText(collocationSkuBean.getOptional_price());
        } else {
            childViewHolder.Complete_Money.setText(collocationSkuBean.getMust_name());
            childViewHolder.Complete_Date.setText(collocationSkuBean.getMust_price());
        }


        return convertView;
    }

    class ParentViewHolder {
        private View v_space;
        private HorizontalScrollView hsv_goods_list;
        TextView Complete_ID;
        TextView Complete_Money;
        TextView Complete_Date;
        TextView Complete_State;
        TextView Complete_Order;
        ImageView Iv_deletedish;

        private ParentViewHolder(View view) {
            v_space = view.findViewById(R.id.v_space);
            hsv_goods_list = (HorizontalScrollView) view.findViewById(R.id.hsv_goods_list);
            Complete_ID = view.findViewById(R.id.Complete_ID);
            Complete_Money = view.findViewById(R.id.Complete_Money);
            Complete_Date = view.findViewById(R.id.Complete_Date);
            Complete_State = view.findViewById(R.id.Complete_State);
            Complete_Order = view.findViewById(R.id.Complete_Order);
            Iv_deletedish=view.findViewById(R.id.iv_deletedish);
        }
    }

    private class ChildViewHolder {
        TextView Complete_ID;
        TextView Complete_Money;
        TextView Complete_Date;
        TextView Complete_State;
        TextView Complete_Order;
        ImageView ivTubiao;


        private ChildViewHolder(View view) {
            Complete_ID = view.findViewById(R.id.Complete_ID);
            Complete_Money = view.findViewById(R.id.Complete_Money);
            Complete_Date = view.findViewById(R.id.Complete_Date);
            Complete_State = view.findViewById(R.id.Complete_State);
            Complete_Order = view.findViewById(R.id.Complete_Order);
            ivTubiao=view.findViewById(R.id.iv_tubiao);
        }
    }

}
